<?php
class Simpple_Controller_Request {
	private $params = array();
	private $keys = array();
	
	const DEFAULT_CONTROLLER = 'index';
	const DEFAULT_ACTION = 'index';
	const DEFAULT_MODULE = 'default';
	
	const METHOD_GET = 'GET';
	const METHOD_POST = 'POST';
	const METHOD_HEAD = 'HEAD';
	const METHOD_PUT = 'PUT';
	
	const CONTROLLER_KEY = 'controller';
	const ACTION_KEY = 'action';
	const MODULE_KEY = 'module';
	
	const AJAX_REQUEST_HEADER = '';
	
	private $previousReq;
	
	public function __construct(){
		
	}
	
	private function _setPreviousRequest(Simpple_Controller_Request $req)
	{
		$this->previousReq = $req;
	}
	
	/**
	 * 获取前一个请求
	 *
	 * @return Simpple_Controller_Request
	 */
	public function getPreviousRequest()
	{
		return $this->previousReq;
	}
	
	/**
	 * 复制一个新的请求出来，并且会自动把当前请求设置为其前一个请求
	 *
	 * @return Simpple_Controller_Request
	 */
	public function copy()
	{
		$req = new Simpple_Controller_Request();
		$req->initKeys($this->keys);
		$req->setParam($this->params);
		$req->_setPreviousRequest($this);
		return $req;
	}
	
	/**
	 * 初始化参数
	 */
	public function initParams($params) {
		$this->params = $params;
	}
	
	/**
	 * 初始化键值
	 *
	 * @param array $keys
	 */
	public function initKeys($keys) {
		$this->keys = $keys;
	}
	
	/**
	 * 获取所有参数
	 *
	 */
	public function getParams() {
		return $this->params;
	}
	
	/**
	 * 设置params
	 *
	 * @param string | array $key
	 * @param mixed $value
	 */
	public function setParam($key, $value = null) {
		if (is_array($key)) {
			foreach($key as $k=>$v) {
				$this->params[$k] = $v;
			}
		} else {
			$this->params[$key] = $value;
		}
	}
	
	/**
	 * 获取控制器名称
	 *
	 * @return string
	 */
	public function getControllerName() {
		return $this->getKey(self::CONTROLLER_KEY, self::DEFAULT_CONTROLLER);
	}
	
	/**
	 * 设置控制器名称
	 *
	 * @param string $name
	 */
	public function setControllerName($name) {
		$this->keys[self::CONTROLLER_KEY] = $name;
	}
	
	/**
	 * 获取action名称
	 *
	 * @return string
	 */
	public function getActionName() {
		return $this->getKey(self::ACTION_KEY, self::DEFAULT_ACTION);
	}
	
	/**
	 * 设置action名称
	 *
	 * @param string $name
	 */
	public function setActionName($name) {
		$this->keys[self::ACTION_KEY] = $name;
	}
	
	
	/**
	 * 获取模块名称
	 *
	 * @return string
	 */
	public function getModuleName() {
		return $this->getKey(self::MODULE_KEY, self::DEFAULT_MODULE);
	}
	
	/**
	 * 获取访问参数
	 *
	 * @param string $key
	 * @param mixed $defVal
	 * @return string
	 */
	public function getParam($key, $defVal = null) {
		if (key_exists($key, $this->params)) {
			return $this->params[$key];
		}
		if (key_exists($key, $this->keys)) {
			return $this->keys[$key];
		}
		return $defVal;
	}
	
	/**
	 * 获取访问key
	 *
	 * @param string $key
	 * @param mixed $defVal
	 * 
	 * @return mixed
	 */
	public function getKey($key, $defVal = null) {
		if (key_exists($key, $this->keys)) {
			return $this->keys[$key];
		}
		return $defVal;
	}
	
	/**
	 * 是否POST请求
	 *
	 * @return bool
	 */
	public function isPost() {
		return strtoupper($_SERVER['HTTP_METHOD']) == self::METHOD_POST;
	}
	
	/**
	 * 是否get请求
	 * 
	 * @return bool
	 */
	public function isGet() {
		return strtoupper($_SERVER['HTTP_METHOD']) == self::METHOD_GET;
	}
	
	/**
	 * 是否ajax请求
	 *
	 */
	public function isAjax() {
		return isset($_SERVER['HTTP_X_REQUESTED_WITH']) 
		&& strtoupper($_SERVER['HTTP_X_REQUESTED_WITH']) == 'XMLHTTPREQUEST';
	}
	
	/**
	 * 是否为HEAD请求
	 *
	 * @return bool
	 */
	public function isHead() {
		return strtoupper($_SERVER['HTTP_METHOD']) == self::METHOD_HEAD;
	}
	
/**
	 * 是否为PUT请求
	 *
	 * @return bool
	 */
	public function isPut() {
		return strtoupper($_SERVER['HTTP_METHOD']) == self::METHOD_PUT;
	}
	
	/**
	 * 重定向网页
	 *
	 * @param string $url
	 */
	public function redirect($url) {
		$moduleName = $this->getModuleName();
		if ($moduleName != self::DEFAULT_MODULE) {
			$url = '/'.$moduleName.'/'.$url;
		}
		header('Location: '.$url);
	}
}
?>